/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.gl;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.gl.Fcperiod;
import src.services.gl.FcperiodModel;
import src.util.Messager;
import src.util.Utils;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class FcperiodBean implements Serializable {

    private List<Fcperiod> fcperiods;
    private Fcperiod selectedData, insertedData;
    private boolean editMode = true;
    private String filter;
   
    public FcperiodBean() {
        this.fcperiods = new FcperiodModel().findAll(null);
        this.insertedData = null;
        this.selectedData = null;
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Fcperiod> getFcperiods() {
        return this.fcperiods;
    }
    
    public Fcperiod getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Fcperiod selectedData) {
        this.selectedData = selectedData;
    }
    
    public Fcperiod getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Fcperiod insertedData) {
        this.insertedData = insertedData;
    }
    
    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void openData() {  
        this.editMode = true;
        this.insertedData = Fcperiod.clone(selectedData);
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Fcperiod();
    }
    
    public void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        
        boolean success = new FcperiodModel().saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.fcperiods.add(insertedData);
            else {
                int i = fcperiods.indexOf(selectedData);
                if (i <= fcperiods.size()) fcperiods.set(i, Fcperiod.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setFcperiodId(null);
        } 
               
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success;
        success = new FcperiodModel().deleteData(this.selectedData);
        
        if (success) {
            this.fcperiods.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatus(success);
        context.addCallbackParam("success", success);  
    }
    
}
